﻿namespace MsForgeReboot.Services {
    #region Usings

    using System.Collections.Generic;
    using System.IO;

    using ImageResizer;

    using MsForgeReboot.Framework.Extensions;
    using MsForgeReboot.Framework.Interfaces;
    using MsForgeReboot.Framework.ViewModels;

    using PagedList;

    #endregion

    public class MediaGalleryService : IMediaGalleryService {
        #region Fields

        private readonly string mediaGalleryFolder;

        #endregion

        #region Constructors and Destructors

        public MediaGalleryService(string mediaGalleryFolder) {
            this.mediaGalleryFolder = mediaGalleryFolder;
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///     Deletes the specified file name.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="subFolder"></param>
        public void Delete(string fileName,string subFolder) {
            DeleteFileIfExists(this.GetFullName(fileName, subFolder));
            DeleteFileIfExists(this.GetThumbnailFullName(fileName, subFolder));
        }

        /// <summary>
        /// Deletes the specified file name.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
         public void Delete(string fileName) {
             Delete(fileName, string.Empty);
         }

        public IPagedList<FileInfo> GetPagedList(int page, int pageSize) {
            var directory = new DirectoryInfo(this.mediaGalleryFolder);
            return directory.GetFiles().ToPagedList(page, pageSize);
        }

        public void Save(ImageViewModel model) {
           Save(model, string.Empty);
        }

        public void Save(ImageViewModel model, string subFolder) {
            string fullName;
            string thumbnailFullName;

            if (string.IsNullOrEmpty(model.Name.GetUrlFriendlyText()))
            {
                fullName = this.GetFullName(model.PostedFile.FileName, subFolder);
                thumbnailFullName = this.GetThumbnailFullName(model.PostedFile.FileName, subFolder);
            }
            else
            {
                fullName =
                    this.GetFullName(model.Name.GetUrlFriendlyText() + Path.GetExtension(model.PostedFile.FileName), subFolder);
                thumbnailFullName =
                    this.GetThumbnailFullName(
                        model.Name.GetUrlFriendlyText() + Path.GetExtension(model.PostedFile.FileName), subFolder);
            }

            this.Delete(fullName, subFolder);
            this.Delete(thumbnailFullName, subFolder);

            model.PostedFile.SaveAs(fullName);

            ImageBuilder.Current.Build(fullName, thumbnailFullName, new ResizeSettings { Width = 200 });
        }

        /// <summary>
        /// Gets the list of all files in the gallery.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<FileInfo> GetList() {
            var directory = new DirectoryInfo(this.mediaGalleryFolder);
            return directory.GetFiles();
        }

        #endregion

        #region Methods

        private static void DeleteFileIfExists(string fullName) {
            if (File.Exists(fullName)) {
                File.Delete(fullName);
            }
        }

        private string GetFullName(string fileName, string subFolder) {

            if (string.IsNullOrEmpty(subFolder)) {
                return string.Format(@"{0}\{1}", this.mediaGalleryFolder, fileName);
            }
            return string.Format(@"{0}\{1}\{2}", this.mediaGalleryFolder, subFolder, fileName);
        }

        private string GetThumbnailFullName(string fileName, string subFolder) {
            if (string.IsNullOrEmpty(subFolder)) {
                return string.Format(@"{0}\Thumbnails\{1}", this.mediaGalleryFolder, fileName);
            }
            return string.Format(@"{0}\{1}\Thumbnails\{2}", this.mediaGalleryFolder, subFolder, fileName);
            

        }

        #endregion
    }
}